package com.tycmc.signelectroniccontracts.logic.renzheng;

import android.content.Intent;

import com.meiya.cunnar.opencloud.livingcertificationkit.LivingCertificationHelper;
import com.meiya.cunnar.opencloud.livingcertificationkit.VerifyCallback;
import com.tycmc.signelectroniccontracts.act.htong.ActHtList;
import com.tycmc.signelectroniccontracts.act.renzheng.ActQiyeRenZheng;
import com.tycmc.signelectroniccontracts.tools.AppData;
import com.tycmc.signelectroniccontracts.tools.base.LogicParent;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.net.result.ProGressUpDataListen;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.FileStore;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class PreQiyeRenZheng extends LogicParent {

    private ActQiyeRenZheng context;
    private QiYeRenZhengInterface pre;

    public PreQiyeRenZheng(QiYeRenZhengInterface pre) {
        this.pre = pre;
        context = ((ActQiyeRenZheng) pre);
        pre.shoujihao(SharePre.getString(SharePreName.modile, ""));
    }

    public void getInfo() {
        RequestAction action = new RequestAction(NetUrl.getCompanyInfo);
        action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
        action.putBody("clientTypeID", SharePre.getString(SharePreName.clientTypeID, ""));
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {

                dialog.dismiss();
                pre.initMap(data);
            }

            @Override
            public void failed(Object data) {
                dialog.dismiss();
                pre.initMap(new HashMap());
            }
        });
    }

    /**
     * 人脸识别
     *
     * @param upMap
     */
    public void renlian(final Map upMap) {
        RequestAction action = new RequestAction(NetUrl.getCunnarToken);
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                huoti(TextShow.valueOf(upMap.get("linkClientName")), TextShow.valueOf(upMap.get("linkClientIdcNo")), upMap, TextShow.valueOf(data.get("token")));
            }

            @Override
            public void failed(Object data) {
                ToastShow.popupToast(data + "");
                dialog.dismiss();
            }
        });
    }

    public void huoti(String name, String idcard, final Map map, String accessToken) {
        LivingCertificationHelper
                .verify(context, /*此处接收姓名*/name, /*此处接收身份证号*/idcard, /*此处接收AccessToken*/accessToken, /*此处接收AppKey*/AppData.appkey,
                        new VerifyCallback() {
                            @Override
                            public void onVerifyCompleted(boolean verify, String msg, byte[] picResult) {
                                LogShow.i("msg", msg + "");
                                if (verify) {
                                    RequestAction action = new RequestAction(NetUrl.savePersonaalFace);
                                    action.putBody("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo, ""));
                                    action.putBody("clientTypeID", SharePre.getString(SharePreName.clientTypeID, ""));
                                    if (null != picResult) {
                                        String save = FileStore.save(System.currentTimeMillis() + ".png", picResult);
                                        File[] files = new File[1];
                                        files[0] = new File(save);
                                        action.putBody("multipartFiles", files);
                                    }
                                    final ProDialog dialog = new ProDialog(context, "");
                                    dialog.show();
                                    new NetWorkUtils(context).upLoadProgress(action, new ProGressUpDataListen() {
                                        @Override
                                        public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {

                                        }

                                        @Override
                                        public void sucess(Map<String, Object> data, String str) {
                                            dialog.dismiss();
                                            map.put("state", "3");
                                            pre.initMap(map);
//
                                        }

                                        @Override
                                        public void failed(Object data) {
                                            ToastShow.popupToast(data + "");
                                            map.put("state", "2");
                                            pre.initMap(map);
                                            dialog.dismiss();
                                        }
                                    });

                                } else {
                                    ToastShow.popupToast(msg);
                                    map.put("state", "2");
                                    pre.initMap(map);
                                }

                            }
                        });
    }

    public interface QiYeRenZhengInterface {
        void shoujihao(String number);

        void initMap(Map map);
    }
}
